home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / dev / c / cxref_1_4a.lha / alloca.c next >
C/C++ Source or Header  |  1997-11-20  |  14KB  |  496 lines

  1. /* alloca.c -- allocate automatically reclaimed memory
  2.    (Mostly) portable public-domain implementation -- D A Gwyn
  3.  
  4.    This implementation of the PWB library alloca function,
  5.    which is used to allocate space off the run-time stack so
  6.    that it is automatically reclaimed upon procedure exit,
  7.    was inspired by discussions with J. Q. Johnson of Cornell.
  8.    J.Otto Tennant <jot@cray.com> contributed the Cray support.
  9.  
  10.    There are some preprocessor constants that can
  11.    be defined when compiling for your specific system, for
  12.    improved efficiency; however, the defaults should be okay.
  13.  
  14.    The general concept of this implementation is to keep
  15.    track of all alloca-allocated blocks, and reclaim any
  16.    that are found to be deeper in the stack than the current
  17.    invocation.  This heuristic does not reclaim storage as
  18.    soon as it becomes invalid, but it will do so eventually.
  19.  
  20.    As a special case, alloca(0) reclaims storage without
  21.    allocating any.  It is a good idea to use alloca(0) in
  22.    your main control loop, etc. to force garbage collection.  */
  23.  
  24. #include <string.h>
  25. #include <stdlib.h>
  26.  
  27. #ifdef emacs
  28. #include "blockinput.h"
  29. #endif
  30.  
  31. /* If compiling with GCC 2, this file's not needed.  */
  32. #if !defined (__GNUC__) || __GNUC__ < 2
  33.  
  34. /* If someone has defined alloca as a macro,
  35.    there must be some other way alloca is supposed to work.  */
  36. #ifndef alloca
  37.  
  38. #ifdef emacs
  39. #ifdef static
  40. /* actually, only want this if static is defined as ""
  41.    -- this is for usg, in which emacs must undefine static
  42.    in order to make unexec workable
  43.    */
  44. #ifndef STACK_DIRECTION
  45. you
  46. lose
  47. -- must know STACK_DIRECTION at compile-time
  48. #endif /* STACK_DIRECTION undefined */
  49. #endif /* static */
  50. #endif /* emacs */
  51.  
  52. /* If your stack is a linked list of frames, you have to
  53.    provide an "address metric" ADDRESS_FUNCTION macro.  */
  54.  
  55. #if defined (CRAY) && defined (CRAY_STACKSEG_END)
  56. long i00afunc ();
  57. #define ADDRESS_FUNCTION(arg) (char *) i00afunc (&(arg))
  58. #else
  59. #define ADDRESS_FUNCTION(arg) &(arg)
  60. #endif
  61.  
  62. #if __STDC__
  63. typedef void *pointer;
  64. #else
  65. typedef char *pointer;
  66. #endif
  67.  
  68. #ifndef NULL
  69. #define    NULL    0
  70. #endif
  71.  
  72. /* Different portions of Emacs need to call different versions of
  73.    malloc.  The Emacs executable needs alloca to call xmalloc, because
  74.    ordinary malloc isn't protected from input signals.  On the other
  75.    hand, the utilities in lib-src need alloca to call malloc; some of
  76.    them are very simple, and don't have an xmalloc routine.
  77.  
  78.    Non-Emacs programs expect this to call use xmalloc.
  79.  
  80.    Callers below should use malloc.  */
  81.  
  82. /*#ifndef emacs*/
  83. /*#define malloc xmalloc*/
  84. /*#endif*/
  85. /*extern pointer malloc ();*/
  86.  
  87. /* Define STACK_DIRECTION if you know the direction of stack
  88.    growth for your system; otherwise it will be automatically
  89.    deduced at run-time.
  90.  
  91.    STACK_DIRECTION > 0 => grows toward higher addresses
  92.    STACK_DIRECTION < 0 => grows toward lower addresses
  93.    STACK_DIRECTION = 0 => direction of growth unknown  */
  94.  
  95. #ifndef STACK_DIRECTION
  96. #define    STACK_DIRECTION    0    /* Direction unknown.  */
  97. #endif
  98.  
  99. #if STACK_DIRECTION != 0
  100.  
  101. #define    STACK_DIR    STACK_DIRECTION    /* Known at compile-time.  */
  102.  
  103. #else /* STACK_DIRECTION == 0; need run-time code.  */
  104.  
  105. static int stack_dir;        /* 1 or -1 once known.  */
  106. #define    STACK_DIR    stack_dir
  107.  
  108. static void
  109. find_stack_direction (void)
  110. {
  111.   static char *addr = NULL;    /* Address of first `dummy', once known.  */
  112.   auto char dummy;        /* To get stack address.  */
  113.  
  114.   if (addr == NULL)
  115.     {                /* Initial entry.  */
  116.       addr = ADDRESS_FUNCTION (dummy);
  117.  
  118.       find_stack_direction ();    /* Recurse once.  */
  119.     }
  120.   else
  121.     {
  122.       /* Second entry.  */
  123.       if (ADDRESS_FUNCTION (dummy) > addr)
  124.     stack_dir = 1;        /* Stack grew upward.  */
  125.       else
  126.     stack_dir = -1;        /* Stack grew downward.  */
  127.     }
  128. }
  129.  
  130. #endif /* STACK_DIRECTION == 0 */
  131.  
  132. /* An "alloca header" is used to:
  133.    (a) chain together all alloca'ed blocks;
  134.    (b) keep track of stack depth.
  135.  
  136.    It is very important that sizeof(header) agree with malloc
  137.    alignment chunk size.  The following default should work okay.  */
  138.  
  139. #ifndef    ALIGN_SIZE
  140. #define    ALIGN_SIZE    sizeof(double)
  141. #endif
  142.  
  143. typedef union hdr
  144. {
  145.   char align[ALIGN_SIZE];    /* To force sizeof(header).  */
  146.   struct
  147.     {
  148.       union hdr *next;        /* For chaining headers.  */
  149.       char *deep;        /* For stack depth measure.  */
  150.     } h;
  151. } header;
  152.  
  153. static header *last_alloca_header = NULL;    /* -> last alloca header.  */
  154.  
  155. /* Return a pointer to at least SIZE bytes of storage,
  156.    which will be automatically reclaimed upon exit from
  157.    the procedure that called alloca.  Originally, this space
  158.    was supposed to be taken from the current stack frame of the
  159.    caller, but that method cannot be made to work for some
  160.    implementations of C, for example under Gould's UTX/32.  */
  161.  
  162. pointer
  163. alloca (unsigned size)
  164. {
  165.   auto char probe;        /* Probes stack depth: */
  166.   register char *depth = ADDRESS_FUNCTION (probe);
  167.  
  168. #if STACK_DIRECTION == 0
  169.   if (STACK_DIR == 0)        /* Unknown growth direction.  */
  170.     find_stack_direction ();
  171. #endif
  172.  
  173.   /* Reclaim garbage, defined as all alloca'd storage that
  174.      was allocated from deeper in the stack than currently. */
  175.  
  176.   {
  177.     register header *hp;    /* Traverses linked list.  */
  178.  
  179. #ifdef emacs
  180.     BLOCK_INPUT;
  181. #endif
  182.  
  183.     for (hp = last_alloca_header; hp != NULL;)
  184.       if ((STACK_DIR > 0 && hp->h.deep > depth)
  185.       || (STACK_DIR < 0 && hp->h.deep < depth))
  186.     {
  187.       register header *np = hp->h.next;
  188.  
  189.       free ((pointer) hp);    /* Collect garbage.  */
  190.  
  191.       hp = np;        /* -> next header.  */
  192.     }
  193.       else
  194.     break;            /* Rest are not deeper.  */
  195.  
  196.     last_alloca_header = hp;    /* -> last valid storage.  */
  197.  
  198. #ifdef emacs
  199.     UNBLOCK_INPUT;
  200. #endif
  201.   }
  202.  
  203.   if (size == 0)
  204.     return NULL;        /* No allocation required.  */
  205.  
  206.   /* Allocate combined header + user data storage.  */
  207.  
  208.   {
  209.     register pointer new = malloc (sizeof (header) + size);
  210.     /* Address of header.  */
  211.  
  212.     if (new == 0)
  213.       abort();
  214.  
  215.     ((header *) new)->h.next = last_alloca_header;
  216.     ((header *) new)->h.deep = depth;
  217.  
  218.     last_alloca_header = (header *) new;
  219.  
  220.     /* User storage begins just after header.  */
  221.  
  222.     return (pointer) ((char *) new + sizeof (header));
  223.   }
  224. }
  225.  
  226. #if defined (CRAY) && defined (CRAY_STACKSEG_END)
  227.  
  228. #ifdef DEBUG_I00AFUNC
  229. #include <stdio.h>
  230. #endif
  231.  
  232. #ifndef CRAY_STACK
  233. #define CRAY_STACK
  234. #ifndef CRAY2
  235. /* Stack structures for CRAY-1, CRAY X-MP, and CRAY Y-MP */
  236. struct stack_control_header
  237.   {
  238.     long shgrow:32;        /* Number of times stack has grown.  */
  239.     long shaseg:32;        /* Size of increments to stack.  */
  240.     long shhwm:32;        /* High water mark of stack.  */
  241.     long shsize:32;        /* Current size of stack (all segments).  */
  242.   };
  243.  
  244. /* The stack segment linkage control information occurs at
  245.    the high-address end of a stack segment.  (The stack
  246.    grows from low addresses to high addresses.)  The initial
  247.    part of the stack segment linkage control information is
  248.    0200 (octal) words.  This provides for register storage
  249.    for the routine which overflows the stack.  */
  250.  
  251. struct stack_segment_linkage
  252.   {
  253.     long ss[0200];        /* 0200 overflow words.  */
  254.     long sssize:32;        /* Number of words in this segment.  */
  255.     long ssbase:32;        /* Offset to stack base.  */
  256.     long:32;
  257.     long sspseg:32;        /* Offset to linkage control of previous
  258.                    segment of stack.  */
  259.     long:32;
  260.     long sstcpt:32;        /* Pointer to task common address block.  */
  261.     long sscsnm;        /* Private control structure number for
  262.                    microtasking.  */
  263.     long ssusr1;        /* Reserved for user.  */
  264.     long ssusr2;        /* Reserved for user.  */
  265.     long sstpid;        /* Process ID for pid based multi-tasking.  */
  266.     long ssgvup;        /* Pointer to multitasking thread giveup.  */
  267.     long sscray[7];        /* Reserved for Cray Research.  */
  268.     long ssa0;
  269.     long ssa1;
  270.     long ssa2;
  271.     long ssa3;
  272.     long ssa4;
  273.     long ssa5;
  274.     long ssa6;
  275.     long ssa7;
  276.     long sss0;
  277.     long sss1;
  278.     long sss2;
  279.     long sss3;
  280.     long sss4;
  281.     long sss5;
  282.     long sss6;
  283.     long sss7;
  284.   };
  285.  
  286. #else /* CRAY2 */
  287. /* The following structure defines the vector of words
  288.    returned by the STKSTAT library routine.  */
  289. struct stk_stat
  290.   {
  291.     long now;            /* Current total stack size.  */
  292.     long maxc;            /* Amount of cont